//
//  RSSwitchButtonView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/11.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "RSSwitchButtonView.h"

@implementation RSSwitchButtonView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andButtonTitle:(NSArray *)array{
    if ([super initWithFrame:frame]) {
        _dataArray = array;
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.buttonClickView];
    [_buttonClickView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@300);
        make.height.equalTo(@44);
    }];
    CGFloat W = 150;
    for (int i = 0; i<_dataArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.tag = 200+i;
        button.frame = CGRectMake(W*i, 0, 150, 44);
        [button setTitle:_dataArray[i] forState:0];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        [button setTitleColor:SELECTEDCOLOR forState:UIControlStateSelected];
        [button addTarget:self action:@selector(titleClick:) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [_buttonClickView addSubview:button];
        if (i == 0) {
            button.selected = YES;
        }
    }
}
- (void)titleClick:(UIButton *)sender{
    for (int i =0; i<_dataArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:i+200];
        btn.selected = NO;
    }
    sender.selected = YES;
    [self.delegate rSSwitchButtonViewSwitch:sender.tag-200];
}
- (UIView *)buttonClickView{
    if (!_buttonClickView) {
        UIView *view = [[UIView alloc]init];
        view.frame = CGRectMake(0, 0, 300, 44);
        _buttonClickView = view;
    }
    return _buttonClickView;
}
@end
